## load packages and data

pacman::p_load(tidyverse, corrr, knitr, kableExtra, haven, fixest, dotwhisker, RColorBrewer, broom)




load("Data/Replication/Kim/Kim_Replication_Ready.rda")



 
### analysis

#Original Base Regressions:

names(data)
m1 <- femlm(Flied_EA ~ navco3yr_sum + navco3yr_other_sum + ln_EA_yrs + yearly, cluster =  "ccode", data=data%>%filter(sample == 1 ), family = "logit") %>% tidy() %>%mutate(model = "Original")
m2 <- femlm(F_RegType_lied_EA ~ navco3yr_sum + navco3yr_other_sum + ln_RegType_lied_n_period_duration + yearly, cluster =  "ccode", data=data%>%filter(RegType_lied_CA == 1 ), family = "logit") %>% tidy() %>%mutate(model = "LIED")
m3 <- femlm(F_RegType_RoW_EA ~ navco3yr_sum + navco3yr_other_sum + ln_RegType_RoW_n_period_duration + yearly, cluster =  "ccode", data=data%>%filter(RegType_RoW_CA == 1 ), family = "logit") %>% tidy() %>%mutate(model = "RoW")
m4 <- femlm(F_Politytype_Anocracy ~ navco3yr_sum + navco3yr_other_sum + ln_Politytype_n_period_duration + yearly, cluster =  "ccode", data=data%>%filter(Politytype_Autocracy == 1 ), family = "logit") %>% tidy() %>%mutate(model = "Polity")
m5 <- femlm(F_status_fh_PF ~ navco3yr_sum + navco3yr_other_sum + ln_fh_status_n_period_duration + yearly, cluster =  "ccode", data=data%>%filter(status_fh_NF == 1 ), family = "logit") %>% tidy() %>%mutate(model = "FH")
m6 <- femlm(F_AnckarRegtype_MP_Autocracy ~ navco3yr_sum + navco3yr_other_sum + ln_AnckarRegtype_n_period_duration + yearly, cluster =  "ccode", data=data%>%filter(AnckarRegtype_Non_MP_Autocracy == 1 ), family = "logit") %>% tidy() %>%mutate(model = "CPR")
m7 <- femlm(F_HTW_RegType_MP_Autocracy ~ navco3yr_sum + navco3yr_other_sum + ln_HTW_RegType_n_period_duration + yearly, cluster =  "ccode", data=data%>%filter(HTW_RegType_Non_MP_Autocracy == 1 ), family = "logit") %>% tidy() %>%mutate(model = "ARD")
m8 <- femlm(F_RegType_magaloni_EA ~ navco3yr_sum + navco3yr_other_sum + ln_RegType_magaloni_n_period_duration + yearly, cluster =  "ccode", data=data%>%filter(RegType_magaloni_CA == 1 ), family = "logit") %>% tidy() %>%mutate(model = "AoW")





summary(m1)
summary(m2)
summary(m3)
summary(m4)
summary(m5)
summary(m6)
summary(m7)
summary(m8)


res <-  rbind(m1,m2,m3, m4,m5,m7,m8,m6)
res_basic <- res %>% mutate(spec = "Base Models")



###Full Regressions


m1 <- femlm(Flied_EA ~ navco3yr_sum + navco3yr_other_sum + 
              ln_EA_yrs + n5_lied_demo + n5_lied_EA + 
              postcold*lnwdi_aid_pc + v2x_libdem + milper_capita + elit_unrest + irreg3yrs + lngdppc + oneyrgrowth + yearly, 
            cluster=  "ccode", data=data%>%filter(sample == 1 ), family = "logit")


summary(m1)

m2 <- femlm(F_RegType_lied_EA ~ navco3yr_sum + navco3yr_other_sum + 
              ln_RegType_lied_n_period_duration + neigh_DEM_lied + neigh_EA_lied + 
              postcold*lnwdi_aid_pc + v2x_libdem + milper_capita + elit_unrest + irreg3yrs + lngdppc + oneyrgrowth + yearly, 
            cluster =  "ccode", data=data%>%filter(RegType_lied_CA == 1), family = "logit")


summary(m2)


m3 <- femlm(F_RegType_RoW_EA ~ navco3yr_sum + navco3yr_other_sum + 
              ln_RegType_RoW_n_period_duration + neigh_DEM_RoW + neigh_EA_RoW + 
              postcold*lnwdi_aid_pc + v2x_libdem + milper_capita + elit_unrest + irreg3yrs + lngdppc + oneyrgrowth + yearly, 
            cluster =  "ccode", data=data%>%filter(RegType_RoW_CA == 1), family = "logit")


summary(m3)



m4 <- femlm(F_Politytype_Anocracy ~ navco3yr_sum + navco3yr_other_sum + 
              ln_Politytype_n_period_duration + neigh_DEM_Polity + neigh_EA_Polity + 
              postcold*lnwdi_aid_pc + v2x_libdem + milper_capita + elit_unrest + irreg3yrs + lngdppc + oneyrgrowth + yearly, 
            cluster =  "ccode", data=data%>%filter(RegType_RoW_CA == 1), family = "logit")


summary(m4)


m5 <- femlm(F_status_fh_PF ~ navco3yr_sum + navco3yr_other_sum + 
              ln_fh_status_n_period_duration + neigh_DEM_FH + neigh_EA_FH + 
              postcold*lnwdi_aid_pc + v2x_libdem + milper_capita + elit_unrest + irreg3yrs + lngdppc + oneyrgrowth + yearly, 
            cluster =  "ccode", data=data%>%filter(status_fh_NF == 1), family = "logit")


summary(m5)

m6 <- femlm(F_AnckarRegtype_MP_Autocracy ~ navco3yr_sum + navco3yr_other_sum + 
              ln_AnckarRegtype_n_period_duration + neigh_DEM_AF + neigh_EA_AF+ 
              postcold*lnwdi_aid_pc + v2x_libdem + milper_capita + elit_unrest + irreg3yrs + lngdppc + oneyrgrowth + yearly, 
            cluster =  "ccode", data=data%>%filter(AnckarRegtype_Non_MP_Autocracy == 1), family = "logit")


summary(m6)



m7 <- femlm(F_HTW_RegType_MP_Autocracy ~ navco3yr_sum + navco3yr_other_sum + 
              ln_AnckarRegtype_n_period_duration + neigh_DEM_HTW + neigh_EA_HTW+ 
              postcold*lnwdi_aid_pc + v2x_libdem + milper_capita + elit_unrest + irreg3yrs + lngdppc + oneyrgrowth + yearly, 
            cluster =  "ccode", data=data%>%filter(HTW_RegType_Non_MP_Autocracy == 1), family = "logit")


summary(m7)





m8 <- femlm(F_RegType_magaloni_EA ~ navco3yr_sum + navco3yr_other_sum + 
              ln_RegType_magaloni_n_period_duration + neigh_DEM_AWD + neigh_EA_AWD+ 
              postcold*lnwdi_aid_pc + v2x_libdem + milper_capita + elit_unrest + irreg3yrs + lngdppc + oneyrgrowth + yearly, 
            cluster =  "ccode", data=data%>%filter(RegType_magaloni_CA == 1), family = "logit")


summary(m8)

m1 <- m1%>% tidy() %>%mutate(model = "Original")
m2 <- m2%>% tidy() %>%mutate(model = "LIED")
m3 <- m3%>% tidy() %>%mutate(model = "RoW")
m4 <- m4%>% tidy() %>%mutate(model = "Polity")
m5 <- m5%>% tidy() %>%mutate(model = "FH")
m6 <- m6%>% tidy() %>%mutate(model = "CPR")
m7 <- m7%>% tidy() %>%mutate(model = "ARD")
m8 <- m8%>% tidy() %>%mutate(model = "AoW")

res <-  rbind(m1,m2,m3, m4,m5,m7,m8,m6)



res$term <- ifelse(res$term == "navco3yr_sum", "Anti-Regime Uprising", res$term)
res$term <- ifelse(res$term == "navco3yr_other_sum", "Other Uprising", res$term)


res_full <- rbind(res_basic, res %>% mutate(spec = "Full Set of Controls"))



res_full$term <- ifelse(res_full$term == "navco3yr_sum", "Anti-Regime Uprising", res_full$term)
res_full$term <- ifelse(res_full$term == "navco3yr_other_sum", "Other Uprising", res_full$term)







res_full <- res_full %>% mutate ( conf.low_95 = estimate - 1.96 *  std.error,
                                  conf.high_95 = estimate + 1.96 * std.error,
                                  conf.low_90 = estimate - 1.64 *  std.error,
                                  conf.high_90 = estimate + 1.64 * std.error)




res_full$Measure <- res_full$model

res_full$Measure <- factor(res_full$Measure,  levels=c("Original", "LIED", "RoW", "Polity", "FH", "ARD", "AoW", "CPR"))
res_full$Measure <- fct_rev(res_full$Measure)

res_full$spec <- as.factor(res_full$spec)

levels(res_full$spec) <- c("Model 1", "Model 3")

### Make Figure K2


ggplot(res_full%>%filter(term %in% c("Anti-Regime Uprising", "Other Uprising" )) %>% mutate(term = fct_rev(term)), #%>%mutate(Measure = fct_rev(model)), 
       aes(x = term, y = estimate, shape = Measure)) +
  geom_hline(yintercept = 0, 
             colour = gray(1/2), lty = 2) +
  geom_linerange(aes(x = term, 
                     ymin = conf.low_95,
                     ymax = conf.high_95), position = position_dodge(width = 1/2), alpha = .7, color = "grey40")  +
  geom_linerange(aes(x = term, 
                     ymin = conf.low_90,
                     ymax = conf.high_90), position = position_dodge(width = 1/2), linewidth = 1, alpha = .7, color = "grey40")  +
  geom_point(aes(x = term, 
                 y = estimate), size = 4, position = position_dodge(width = 1/2), color = "black") +
  ggtitle("") + ylab ("") + xlab("") + facet_wrap(~spec) +
  coord_flip()  + 
  theme_classic(base_size = 20) + theme(text=element_text(size=20)) +  guides(shape = guide_legend(reverse=T), title = "Measure") +
  theme(legend.position = "right") + scale_shape_manual(values = c(56:49))






